<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
</head>
<body>
<div class="layui-fluid" style="padding: 15px;">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="test-table-reload-btn" style="margin-bottom: 10px;">
                <form id="bil-express-state-update-form" class="layui-form" lay-filter="bil-express-state-update-form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-inline">
                                <select name="state" lay-filter="bil-express-state-update-form-state-select">
                                    <option value="">请选择</option>
                                    <option value="4">已发往国内</option>
                                    <option value="5">已到达海关,清关中</option>
                                    <option value="6">海关抽检中</option>
                                    <option value="7">清关完毕</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button id="bil-express-state-update-btn" type="button" class="layui-btn">更新</button>
                        </div>
                    </div>
                </form>
            </div>

            <table class="layui-hide" id="bil-express-state-update-table"
                   lay-filter="bil-express-state-update-table"></table>

            运单的重量或者费用任何一个为0时，该运单将不显示在列表中。
        </div>
    </div>
</div>

<script type="text/html" id="bil-express-state-update-table-state-tmp">
    {{# if(d.state === 1) { }} <!-- 运单创建成功 -->
    运单创建成功
    {{# } else if(d.state === 2) { }}<!--  -->
    分店已收件
    {{# } else if(d.state === 3) { }}<!--  -->
    快件中心已揽收
    {{# } else if(d.state === 4) { }}<!--  -->
    已发往国内
    {{# } else if(d.state === 5) { }}<!--  -->
    已到达海关,清关中
    {{# } else if(d.state === 6) { }}<!--  -->
    海关抽检中
    {{# } else if(d.state === 7) { }}<!--  -->
    清关完毕
    {{# } }}
</script>

<script src="/layui/layui.js"></script>
<script>
    layui.config({
        base: '/src/',
        version: new Date().getTime()
    }).extend({setter: "config", admin: "lib/admin", view: "lib/view"})
        .define(['setter', 'admin', 'table', "form"], function () {
            var $ = layui.$,
                admin = layui.admin,
                form = layui.form,
                table = layui.table;

            table.render({
                id: "bil-express-state-update-table",
                elem: '#bil-express-state-update-table',
                url: '/bil/listBilExpress',
                cols: [[
                    {type: 'checkbox'},
                    {field: 'id', title: 'ID', width: 80, hide: true},
                    {field: 'waybillNo', title: '运单号'},
                    {field: 'channelCode', title: '渠道', hide: true},
                    {field: 'channelName', title: '渠道'},
                    {field: 'storeCode', title: '仓库', hide: true},
                    {field: 'storeName', title: '仓库'},
                    {field: 'receiverId', title: '收件人', hide: true},
                    {field: 'receiverName', title: '收件人'},
                    {field: 'state', title: '状态', templet: '#bil-express-state-update-table-state-tmp'},
                    {field: 'createTime', title: '订单时间'}
                ]],
                page: true,
                limit: 8,
                where: {
                    total: -1,
                    access_token: layui.data('layuiAdmin').access_token
                }
            });

            form.on('select(bil-express-state-update-form-state-select)', function (data) {
                var value = data.value; //得到被选中的值
                if (value.length !== 0) {
                    if(value === "7") {
                        value = "5,6";
                    } else{
                        value = (parseInt(value) - 1).toString();
                    }

                    table.reload("bil-express-state-update-table", {
                        where: {"states": value, "access_token": layui.data('layuiAdmin').access_token},
                        done: function () {
                            layer.closeAll('loading');
                        }
                    });
                }
            });

            $("#bil-express-state-update-btn").off("click").on("click", function () {
                var state = $("#bil-express-state-update-form select[name=state]").val();
                if(state.length === 0) {
                    layer.msg("请选择状态", {icon: 5, time: 1500});
                    return;
                }

                var checkStatus = table.checkStatus('bil-express-state-update-table');
                if(checkStatus.data.length === 0) {
                    layer.msg("请选择数据", {icon: 5, time: 1500});
                    return;
                }

                var data = {"state": state};
                var waybillNos = "";
                $.each(checkStatus.data, function(index, item) {
                    waybillNos = waybillNos + "," + item.waybillNo;
                });

                waybillNos = waybillNos.substring(1);
                data["waybillNos"] = waybillNos;

                admin.req({
                    url: "/bil/batchUpdateBilExpressState",
                    data: data,
                    beforeSend: function () {
                        layer.load(2, {time: 100 * 1000});
                    },
                    complete: function () {
                        layer.closeAll('loading');
                    },
                    done: function () {
                        form.val("bil-express-state-update-form", {"state": ""});
                        table.reload("bil-express-state-update-table", {
                            where: {"state": parseInt(state) - 1, "access_token": layui.data('layuiAdmin').access_token},
                            done: function () {
                                layer.closeAll('loading');
                            }
                        });
                    }
                });
            });
        });
</script>
</body>
</html>